﻿@{
    PageData["Title"] = "Order Succeeded!";

    if (!IsPost) {
        Response.Redirect("~/Products");
    }

    var db = Database.Open("bakery");
    var productId = Request["ProductId"];
    var product = db.QuerySingle("SELECT * FROM Products WHERE ID = @0", productId);
}

<div class="receipt">
@{
    var body = "Thank you, we have received your order for " + Request["orderQty"] + " unit(s) of " + product.Name + "!<br/>";
    var orderShipping = Request["orderShipping"];
    var customerEmail = Request["orderEmail"];
    if (!orderShipping.IsEmpty()) {
        //Replace carriage returns with HTML breaks for HTML mail
        var formattedOrder = orderShipping.Replace("\r\n", "<br/>");
        body += "Your address is: <br/>" + formattedOrder + "<br/>";
    }
    body += "Your total is $" + product.Price * Request["orderQty"].AsDecimal() +".<br/>";
    body += "We will contact you if we have questions about your order.  Thanks!<br/>";

    //SMTP Configuration for Hotmail
    WebMail.SmtpServer = "smtp.live.com";
    WebMail.SmtpPort = 25;
    WebMail.EnableSsl = true;

    //Enter your Hotmail credentials for UserName/Password and a "From" address for the e-mail
    WebMail.UserName = "";
    WebMail.Password = "";
    WebMail.From = "";

    if (WebMail.UserName.IsEmpty() || WebMail.Password.IsEmpty() || WebMail.From.IsEmpty()) {
        <h2>Please set up Mail!</h2>
        <p>Please set your Hotmail Username, Password, and From Address in OrderSuccess.cshtml to send mail</p>
    } else {
        WebMail.Send(to: customerEmail, subject: "New Order", body: body);
        <h2>Thank you for ordering!</h2>
        <p>We've sent e-mail to @customerEmail with your order details.  Enjoy!</p>
    }    
}
</div>
